#include <iostream>
#include <vector>
using namespace std;

class App {
	int cnt = 1;
        long vertex = 0;
	vector<int> head, next, to, weight;
	vector<long> cow, dist, size;
	long best = INT32_MAX, center = 0;

        void addedge(int u, int v, int w)
        {
                next[cnt] = head[u];
                to[cnt] = v;
                weight[cnt] = w;
                head[u] = cnt++;
        }

        void dfs(int u, int p)
        {
                size[u] = cow[u];
                long maxsub = 0;
                for(int e = head[u], v = to[e]; e; e = next[e], v = to[e]){
                        if(v != p){
                                dfs(v, u);
                                size[u] += size[v];
                                maxsub = max(maxsub, size[v]);
                        }
                }
                maxsub = max(maxsub, vertex - size[u]);
                if(maxsub < best){
                        best = maxsub;
                        center = u;
                }
        }

        void dfs(int u, int p, int w)
        {
                dist[u] = dist[p] + w;
                for(int e = head[u], v = to[e]; e; e = next[e], v = to[e]){
                        if(v != p){
                                dfs(v, u, weight[e]);
                        }
                }
        }

    public:
	App(int n) : head(n + 1, 0), next(n << 1, 0), to(n << 1, 0), weight(n << 1, 0), cow(n + 1, 0), dist(n + 1, 0), size(n + 1, 0)
	{
                for(int i = 1; i <= n; i++){
                       cin >> cow[i];
                       vertex += cow[i];
                }

                for(int i = 1, u, v, w; i <= n - 1; i++){
                        cin >> u >> v >> w;
                        addedge(u, v, w);
                        addedge(v, u, w);
                }

                dfs(1, 0);
                dfs(center, 0, 0);
                long long ans = 0;
                for(int i = 1; i <= n; i++){
                        ans += cow[i] * dist[i];
                }
                cout << ans << endl;
	}
};

int main()
{
        int n; 
        cin >> n;
        App app(n);
        return 0;
}